﻿<?php
session_start();
include_once "config.php";

$current_url = urlencode($url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
?>

<html>
<head>
    <title>购物车</title>
    <link href="style/style.css" rel="stylesheet" type="text/css">
</head>
<body>

<h1 align="center">商品 </h1>

<!-- 购物车 -->
<?php
if (isset($_SESSION["cart_products"]) && count($_SESSION["cart_products"]) > 0) {
    echo '<div class="cart-view-table-front" id="view-cart">';
    echo '<h3>您的购物车</h3>';
    echo '<form method="post" action="cart_update.php">';
    echo '<table width="100%"  cellpadding="6" cellspacing="0">';
    echo '<tbody>';
    $total = 0;
    $b = 0;
    /*
     *$_SESSION数组需要存放“用户名”、“购物车商品”等数据，
     * 本例$_SESSION["cart_products"]存放“购物车商品”，信息如下表，
     * 因此$_SESSION["cart_products"] 是一个二维数组，
     * $_SESSION则是一个三维数组（关联数组）。
     * 其中，$_SESSION["cart_products"]["product_code"]元素是一个一维数组，
     * 对应下表中每一行
     *                       $_SESSION["cart_products"]
     * -------------------------------------------------------------------
     * |product_color|product_qty|product_code|product_name|product_price|
     * -------------------------------------------------------------------
     * |             |           |            |            |             |
     * -------------------------------------------------------------------
     * |             |           |            |            |             |
     * -------------------------------------------------------------------
     * |             |           |            |            |             |
     * -------------------------------------------------------------------
     * */
    foreach ($_SESSION["cart_products"] as $cart_itm)//$cart_itm是个一维数组
    {
        $product_name = $cart_itm["product_name"];
        $product_qty = $cart_itm["product_qty"];
        $product_price = $cart_itm["product_price"];
        $product_code = $cart_itm["product_code"];
        $product_color = $cart_itm["product_color"];
        $bg_color = ($b++ % 2 == 1) ? 'odd' : 'even'; //奇偶行背景色
        echo '<tr class="' . $bg_color . '">';
        //有多个商品的数量被更新，用数组方式提交表单数据，服务器端需要商品码和数量，用关联数组
        echo '<td>数量<input type="text" size="2" maxlength="2" name="product_qty[' . $product_code . ']" value="' . $product_qty . '" /></td>';
        echo '<td>' . $product_name . '</td>';
        //有多个商品被退订，用数组方式提交表单数据，服务器端只需商品码，用索引数组即可
        echo '<td><input type="checkbox" name="remove_code[]" value="' . $product_code . '" /> 退订</td>';
        echo '</tr>';
        $subtotal = ($product_price * $product_qty);
        $total = ($total + $subtotal);
    }
    echo '<td colspan="4">';
    echo '<button type="submit">确认退订</button><a href="view_cart.php" class="button">结账</a>';
    echo '</td>';
    echo '</tbody>';
    echo '</table>';

    $current_url = urlencode($url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
    echo '<input type="hidden" name="return_url" value="' . $current_url . '" />';
    echo '</form>';
    echo '</div>';
}

?>
<!-- 商品展示 -->
<?php
$query = "SELECT product_code, product_name, product_desc, product_img_name, price FROM products ";//ORDER BY id ASC
$stmt = $dbh->query($query);

if ($stmt->rowCount() > 0) {
    $products_item = '<ul class="products">';
    while ($obj = $stmt->fetchObject()) {
        $products_item .= <<<EOT
	<li class="product">
	<form method="post" action="cart_update.php">
	<div class="product-content"><h3>{$obj->product_name}</h3>
	<div class="product-thumb"><img src="images/{$obj->product_img_name}"></div>
	<div class="product-desc">{$obj->product_desc}</div>
	<div class="product-info">
	价格 {$obj->price} 
	
	<fieldset>
	
	<label>
		<span>颜色</span>
		<select name="product_color">
		<option value="Black">黑色</option>
		<option value="Silver">白色</option>
		</select>
	</label>
	
	<label>
		<span>数量</span>
		<input type="text" size="2" maxlength="2" name="product_qty" value="1" />
	</label>
	
	</fieldset>
	<input type="hidden" name="product_code" value="{$obj->product_code}" />
	<input type="hidden" name="type" value="add" />
	<input type="hidden" name="return_url" value="{$current_url}" />
	<div align="center"><button type="submit" class="add_to_cart">加入购物车</button></div>
	</div></div>
	</form>
	</li>
EOT;
    }
    $products_item .= '</ul>';
    echo $products_item;
}

?>

</body>
</html>
